<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// vue文件中引入echarts工具
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { mapGetters } from 'vuex'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    },
    'sidebar.opened': {
      handler(nV, oV) {
        const vm = this
        // 加定时器 因为伸缩动画有动画时长，在动画结束后重新渲染echart
        setTimeout(function() {
          vm.chart.resize()
        }, 200)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      // // 自动适应窗口大小
      // window.addEventListener('resize', () => {
      //   this.chart.resize()
      // })
      window.addEventListener('resize', () => { this.chart.resize() })
    })
  },
  init() {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
    window.addEventListener('resize', () => { this.chart.resize() })
  },
  computed: {
    ...mapGetters([
      'sidebar'
    ])// 获取左侧导航栏对象（其中opened为导航栏伸缩属性）
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ fundCode, dateData, indexData, profitData } = {}) {
      this.chart.setOption({
        title: {
          show: true,
          text: '指数趋势投资收益对比图',
          left: 'center',
          textStyle: {
            color: '#333333',
            fontWeight: 'bold'
          }
        },
        xAxis: {
          data: dateData,
          boundaryGap: false,
          axisTick: {
            show: true
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 10,
          top: 60,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [10, 10]
        },
        yAxis: {
          axisTick: {
            show: true
          },
          scale: true // y轴不从0开始
        },
        legend: {
          data: [fundCode, '趋势投资'],
          top: '30'
        },
        series: [{
          name: fundCode,
          itemStyle: {
            normal: {
              color: '#FF005A',
              lineStyle: {
                color: '#FF005A',
                width: 1
              }
            }
          },
          smooth: true,
          type: 'line',
          data: indexData,
          animationDuration: 1500,
          animationEasing: 'cubicInOut'
        },
        {
          name: '趋势投资',
          smooth: true,
          type: 'line',
          itemStyle: {
            normal: {
              color: '#409EFF',
              lineStyle: {
                color: '#409EFF',
                width: 1
              }
              // areaStyle: {
              //   color: '#f3f8ff'
              // }
            }
          },
          data: profitData,
          animationDuration: 1500,
          animationEasing: 'quadraticOut'
        }]
      })
    }
  }
}
</script>
